const express = require('express');
const app = express();

// 导入cors解决跨域
const cors = require('cors');
app.use(cors());
// 设置响应数据的中间件
app.use((req, res, next) => {
  // status = 0是成功，1是失败，默认为失败
  res.cc = (err, status = 1) => {
    res.send({
      status,
      // 判断err是错误对象还是字符串
      message: err instanceof Error ? err.message : err,
    });
  };
  next();
});

// 配置post解析中间件
app.use(express.urlencoded({ extended: false }));
// 导入路由模块
const userRouter = require('./router/index');
app.use('/api', userRouter);

app.listen(8008, () => {
  console.log('http://localhost:8008');
});
